from audioop import reverse

from django.http import HttpResponseBadRequest
from django.shortcuts import render, HttpResponseRedirect, redirect
from django.views import *

from home.models import *

class IndexView(View):
    def get(self,request):
        #获取所有分类信息数据
        articleCategories = ArticleCategory.objects.all()
        #接受用户点击的分类id
        cat_id = request.GET.get('cat_id',1)
        #根据分类id进行分类的查询
        try:
            select_articleCategory = ArticleCategory.objects.get(id=cat_id)
        except ArticleCategory.DoesNotExist:
            select_articleCategory = ArticleCategory.objects.get(id=1)
        articles = Article.objects.filter(category=select_articleCategory)
        art = Article.objects.filter(category=select_articleCategory).order_by('-total_views')[:2]
        #获取分页参数
        page_index = request.GET.get('page_index',1)#页码
        page_size = request.GET.get('page_size',2)#页容量
        #根据分类信息查询该分类下的所有文章数据
        #创建分页器
        from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger
        pages = Paginator(articles,page_size)#对查询到的数据对象articles进行分页，设置超过指定也容量就分页
        try:
            list = pages.page(page_index)#获取当前页面的记录
        except PageNotAnInteger:
            list = pages.page(pages.num_pages)#如果用户输入的页数不在系统的页码列表中时，显示最后一页的内容
        except EmptyPage:
            list = pages.page(pages.num_pages)

        context = {
            'articleCategories': articleCategories,
            'selectArticleCategory': select_articleCategory,
            'articles':list,
            'cat_id':cat_id,
            'art':art
        }
        print(list)
        return render(request, 'index.html', context=context)

class DetailView(View):
    def get(self,req):
        art_id = req.GET.get('artid')
        try:
            article = Article.objects.get(id = art_id)
        except Article.DoesNotExist:
            return render(req,'404.html')
        #浏览量简单算法：只要被查询一次，那么就算一次访问
        article.total_views += 1
        article.save()
        # 重新查询文章信息，按照浏览量降序排序（热门标签）
        #获取所有评论信息
        comm = Comment.objects.filter(article=article).order_by('-created_time')
        artc = Article.objects.all().order_by('-create_time')[:3]
        #返回页面
        context = {
            'article':article,
            'comms':comm,
            'artc':artc,
        }
        return render(req, 'details.html', context=context)
    def post(self, request):
        # 1获取已登录用户信息
        user = request.user
        # 2 判断用户是否登录
        if user and user.is_authenticated:
            # 3 登录用户才可以接受form数据
            # 3-1 接收评论数据
            art_id = request.POST.get('art_id')
            content = request.POST.get('content')
            # 3-2 验证文章是否存在
            try:
                art = Article.objects.get(id=art_id)
            except Article.DoesNotExist:
                return HttpResponseBadRequest('该文章不存在')
            # 3-3 保存平路数据
            Comment.objects.create(content=content, article=art, user=user)
            # 3-4 修改文章的评论数量
            art.comments_count += 1
            art.save()
            # 刷新当前页面
            request_url = request.path + '?artid=' + art_id
            return redirect(request_url)
        else:
            # 4未登录用户则跳转到登录页面
            return redirect(reverse('hopublog:login'))




